package com.twetec.cms.adapter.html.frondend.response;

import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

/**
 * 封装返回给客户端的结果
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
public class R<T> implements Serializable {

    /**
     * 响应码
     */
    @ApiModelProperty(value = "响应状态码", example = "20000")
    private Integer code;

    /**
     * 响应信息
     */
    @ApiModelProperty(value = "响应信息", example = "success")
    private String msg;

    /**
     * 响应数据
     */
    @ApiModelProperty("响应数据")
    private T data;


    private R() {}

    /**
     *  响应成功，默认
     */
    public static R success() {
        R result = new R();

        result.setCode(ResultCodeMsgEnum.SUCCESS.getCode());
        result.setMsg(ResultCodeMsgEnum.SUCCESS.getMsg());

        return result;
    }

    /**
     *  响应失败，默认
     */
    public static R failure() {
        R result = new R();

        result.setCode(ResultCodeMsgEnum.FAILURE.getCode());
        result.setMsg(ResultCodeMsgEnum.FAILURE.getMsg());

        return result;
    }

    /**
     * 用户鉴权失败
     */
    public static R failureUserAuth() {
        R result = new R();

        result.setCode(ResultCodeMsgEnum.FAILURE_USER_AUTH.getCode());
        result.setMsg(ResultCodeMsgEnum.FAILURE_USER_AUTH.getMsg());

        return result;
    }

    /**
     * 请求参数校验失败
     */
    public static R failureRequestParam() {
        R result = new R();

        result.setCode(ResultCodeMsgEnum.REQUEST_PARAM_ERROR.getCode());
        result.setMsg(ResultCodeMsgEnum.REQUEST_PARAM_ERROR.getMsg());

        return result;
    }

    /**
     * 请求参数缺失
     */
    public static R failureRequestParamMiss() {
        R result = new R();

        result.setCode(ResultCodeMsgEnum.REQUEST_PARAM_MISS_ERROR.getCode());
        result.setMsg(ResultCodeMsgEnum.REQUEST_PARAM_MISS_ERROR.getMsg());

        return result;
    }

    /**
     * 请求方式错误
     */
    public static R failureRequestMethod() {
        R result = new R();

        result.setCode(ResultCodeMsgEnum.REQUEST_METHOD_ERROR.getCode());
        result.setMsg(ResultCodeMsgEnum.REQUEST_METHOD_ERROR.getMsg());

        return result;
    }

    /**
     * 方法,参数类型不匹配
     */
    public static R failureRequestParamType() {
        R result = new R();

        result.setCode(ResultCodeMsgEnum.REQUEST_PARAM_TYPE_ERROR.getCode());
        result.setMsg(ResultCodeMsgEnum.REQUEST_PARAM_TYPE_ERROR.getMsg());

        return result;
    }

    /**
     * 数据库操作失败
     */
    public static R serviceError() {
        R result = new R();

        result.setCode(ResultCodeMsgEnum.SERVICE_ERROR.getCode());
        result.setMsg(ResultCodeMsgEnum.SERVICE_ERROR.getMsg());

        return result;
    }

    /**
     *  直接设置code和msg
     */
    public static R resultCodeMsg(ResultCodeMsgEnum resultCodeMsgEnum, String ...arges) {
        R result = new R();

        result.setCode(resultCodeMsgEnum.getCode());

        String format = String.format(resultCodeMsgEnum.getMsg(), arges);
        result.setMsg(format);

        return result;
    }

    public R code(Integer code) {
        this.code = code;
        return this;
    }

    public R msg(String msg) {
        this.msg = msg;
        return this;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public R data(T data) {
        this.data = data;
        return this;
    }
}
